package com.klhappy.houyuan.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ThumbnailUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CompressPictureUtils {

	/**
	 * 仅压缩分辨率
	 */
	public static void transImageWithPX(String fromFile, String toFile, int width, int height) {
		try {
			Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
			
			int bitmapWidth = bitmap.getWidth();
			int bitmapHeight = bitmap.getHeight();
			// 缩放图片的尺寸
			float scaleWidth = (float) width / bitmapWidth;
			float scaleHeight = (float) height / bitmapHeight;
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			// 产生缩放后的Bitmap对象
			Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);
			
			// save file
			File myCaptureFile = new File(toFile);
			FileOutputStream out = new FileOutputStream(myCaptureFile);
			if(resizeBitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
				out.flush();
				out.close();
			}
			if(!bitmap.isRecycled()) {
				bitmap.recycle();//记得释放资源，否则会内存溢出
			}
			if(!resizeBitmap.isRecycled()) {
				resizeBitmap.recycle();
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * 仅压缩质量
	 */
	public static void transImageWithQuality(String fromFile, String toFile, int quality) {
		try {
			Bitmap bitmap = BitmapFactory.decodeFile(fromFile);
			// save file
			File myCaptureFile = new File(toFile);
			FileOutputStream out = new FileOutputStream(myCaptureFile);
			if(bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {
				out.flush();
				out.close();
			}
			if(!bitmap.isRecycled()) {
			bitmap.recycle();//记得释放资源，否则会内存溢出
			}
			if(!bitmap.isRecycled()) {
				bitmap.recycle();
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 压缩分辨率和质量，参数自调
	 * 
	 * @param fromFile 源文件
	 * @param toFile 输出文件
	 * @param px 目标像素大小（宽*高）
	 * @param size 源文件大小
	 */
	public static void transImage(String fromFile, String toFile, int px, int size) {
		int quality = 93;//压缩质量设置
		try {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(fromFile, options);
			//压缩分辨率设置
			int bitmapWidth = options.outWidth;
			int bitmapHeight = options.outHeight;
			// 缩放图片的尺寸
			float scaleWidth = 1;
			float scaleHeight = 1;
			int originalPx = bitmapWidth * bitmapHeight;
			if (originalPx > px) {
				scaleWidth = (float) Math.sqrt(1.0 * px/originalPx);
				scaleHeight = (float) Math.sqrt(1.0 * px/originalPx);
			}
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);

			// 计算缩放比
			int h = options.outHeight;
			int w = options.outWidth;
			float tWidth =  w * scaleWidth;
			float tHeight = h * scaleHeight;

			int be = (int)(w / tWidth + 0.5f);
			if (be <= 0) {
				be = 1;
			}else if (w / tWidth > 1 && w / tWidth< 2) {
				be = 2;
			}
			options.inJustDecodeBounds = false;
			options.inSampleSize = be;
			if (be == 1) {
				quality = 90;
			}
			// 重新读入图片，读取缩放后的bitmap，注意这次要把options.inJustDecodeBounds 设为 false
			Bitmap resizeBitmap = BitmapFactory.decodeFile(fromFile, options);
			// 利用ThumbnailUtils来创建缩略图，这里要指定要缩放哪个Bitmap对象
			resizeBitmap = ThumbnailUtils.extractThumbnail(resizeBitmap, (int) tWidth, (int) tHeight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

			// 保存文件
			File myCaptureFile = new File(toFile);
			FileOutputStream out = new FileOutputStream(myCaptureFile);
			if(resizeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)) {
				out.flush();
				out.close();
			}
			if(!resizeBitmap.isRecycled()) {
				resizeBitmap.recycle();
				resizeBitmap = null;
			}
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
}
